package com.zhny.project.count.algorithm.service.impl;

import com.zhny.common.constant.UserConstants;
import com.zhny.common.utils.DateUtils;
import com.zhny.common.utils.StringUtils;
import com.zhny.common.utils.security.ShiroUtils;
import com.zhny.common.utils.text.Convert;
import com.zhny.project.count.algorithm.domain.Algorithm;
import com.zhny.project.count.algorithm.mapper.AlgorithmMapper;
import com.zhny.project.count.algorithm.service.IAlgorithmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 算法Service业务层处理
 * 
 * @author lxh
 * @date 2019-09-24
 */
@Service
public class AlgorithmServiceImpl implements IAlgorithmService 
{
    @Autowired
    private AlgorithmMapper algorithmMapper;

    /**
     * 查询算法
     * 
     * @param algorithmKey 算法ID
     * @return 算法
     */
    @Override
    public Algorithm selectAlgorithmById(String algorithmKey)
    {
        return algorithmMapper.selectAlgorithmById(algorithmKey);
    }

    /**
     * 查询算法列表
     * 
     * @param algorithm 算法
     * @return 算法
     */
    @Override
    public List<Algorithm> selectAlgorithmList(Algorithm algorithm)
    {
        return algorithmMapper.selectAlgorithmList(algorithm);
    }

    /**
     * 新增算法
     * 
     * @param algorithm 算法
     * @return 结果
     */
    @Override
    public int insertAlgorithm(Algorithm algorithm)
    {

        algorithm.setAlgorithmKey(UUID.randomUUID().toString());
        algorithm.setCreateBy(ShiroUtils.getUserId().toString());
        algorithm.setCreateTime(DateUtils.getNowDate());
        return algorithmMapper.insertAlgorithm(algorithm);
    }

    /**
     * 修改算法
     * 
     * @param algorithm 算法
     * @return 结果
     */
    @Override
    public int updateAlgorithm(Algorithm algorithm)
    {
        algorithm.setUpdateTime(DateUtils.getNowDate());
        return algorithmMapper.updateAlgorithm(algorithm);
    }

    /**
     * 删除算法对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAlgorithmByIds(String ids)
    {
        return algorithmMapper.deleteAlgorithmByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除算法信息
     * 
     * @param algorithmKey 算法ID
     * @return 结果
     */
    public int deleteAlgorithmById(String algorithmKey)
    {
        return algorithmMapper.deleteAlgorithmById(algorithmKey);
    }

    //    校验ID
    public String checkalgorithmId(Algorithm algorithm)
    {
        String algorithmKey = algorithm.getAlgorithmKey();
        Algorithm info = algorithmMapper.checkalgorithmId(algorithm.getAlgorithmId());
        if (StringUtils.isNotNull(info) && !info.getAlgorithmKey().equals(algorithmKey))
        {
            return UserConstants.POST_NAME_NOT_UNIQUE;
        }
        return UserConstants.POST_NAME_UNIQUE;
    }
    //    校验Name
    public String checkalgorithmName(Algorithm algorithm)
    {
        String algorithmKey = algorithm.getAlgorithmKey();
        Algorithm info = algorithmMapper.checkalgorithmName(algorithm.getAlgorithmName());
        if (StringUtils.isNotNull(info) && !info.getAlgorithmKey().equals(algorithmKey))
        {
            return UserConstants.POST_NAME_NOT_UNIQUE;
        }
        return UserConstants.POST_NAME_UNIQUE;
    }
}
